package com.yanqu.road.server.cmd.children;

import com.yanqu.road.entity.child.ChildTrainResult;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.child.ChildProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.ChildModule;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_CHILD_TRAIN, desc = "子女培养")
public class ChildTrainCmd extends AbstractCommand {

    private int siteIndex;

    private String childUid;

    private int type;

    private int trainTimes;

    private ChildTrainResult result;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ChildProto.ChildTrainReqMsg reqMsg = ChildProto.ChildTrainReqMsg.parseFrom(packet.getBytes());
        siteIndex = reqMsg.getSiteIndex();
        childUid = reqMsg.getChildUid();
        type = reqMsg.getType();
        trainTimes = reqMsg.getTrainTimes();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (player == null) {
            return;
        }
        result = player.getModule(ChildModule.class).childTrain(siteIndex, childUid, type, trainTimes, childUid);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(null != result) {
            ChildProto.ChildTrainRespMsg.Builder respMsg = ChildProto.ChildTrainRespMsg.newBuilder();
            respMsg.setRet(result.getRet());
            if(result.getRet() == 0){
                respMsg.setEffectMammonSkill(result.getEffectMammonSkill());
                respMsg.setEffectTimes(result.getEffectTimes());
            }
            player.sendPacket(Protocol.U_CHILD_TRAIN, respMsg);
        }
    }
}
